﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XCoder
{
    /// <summary>
    /// 索引列架构信息
    /// </summary>
    internal class XIndexColumn
    {
        /// <summary>
        /// 获取或设置表名称
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 获取或设置列名称
        /// </summary>
        public string ColumnName { get; set; }

        /// <summary>
        /// 获取或设置索引名称
        /// </summary>
        public string IndexName { get; set; }

        /// <summary>
        /// 是否为主键
        /// </summary>
        public bool IsPrimaryKey
        {
            get
            {
                return !string.IsNullOrEmpty(this.IndexName)
                    && this.IndexName.Length > 1
                    && this.IndexName[0] == 'P' &&
                    this.IndexName[1] == 'K';
            }
        }

        /// <summary>
        /// 将数据行转换为索引列架构信息
        /// </summary>
        /// <param name="row"> 数据行 </param>
        /// <param name="dbType"> 数据库类型 </param>
        /// <returns></returns>
        public static XIndexColumn Parse(System.Data.DataRow row, System.Data.DatabaseType dbType)
        {
            XIndexColumn model = new XIndexColumn();
            if (row != null)
            {
                switch (dbType)
                {
                    case System.Data.DatabaseType.SqlServer:
                        #region SqlServer赋值

                        //  表名称
                        if (row.Table.Columns.Contains("Table_Name"))
                            model.TableName = row["Table_Name"] as string;
                        //  列名称
                        if (row.Table.Columns.Contains("Column_Name"))
                            model.ColumnName = row["Column_Name"] as string;
                        //  索引名称
                        if (row.Table.Columns.Contains("Index_Name"))
                            model.IndexName = row["Index_Name"] as string;

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            return model;
        }

        /// <summary>
        /// 将数据源转换为索引列架构集合信息
        /// </summary>
        /// <param name="source"> 数据源 </param>
        /// <param name="dbType"> 数据库类型 </param>
        /// <returns></returns>
        public static System.Collections.Generic.List<XIndexColumn> Parse(System.Data.DataTable source, System.Data.DatabaseType dbType)
        {
            System.Collections.Generic.List<XIndexColumn> models = new System.Collections.Generic.List<XIndexColumn>();
            if (source != null && source.Rows.Count > 0)
            {
                foreach (System.Data.DataRow row in source.Rows)
                {
                    models.Add(XIndexColumn.Parse(row, dbType));
                }
            }
            return models;
        }
    }
}
